package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 * @version v1.0
 * @date 2019/10/17 19:32
 * @description TODO
 **/
@RestController
@RequestMapping("/report")
public class ReportController {

    public static final Logger log = Logger.getLogger(ReportController.class);

    @Reference
    private MemberService memberService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH, -12);//定位到一年前

        //月份数据
        List<String> queryDataParm = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            instance.add(Calendar.MONTH,1);
            String data=new SimpleDateFormat("yyyy.MM").format(instance.getTime());
            queryDataParm.add(data);
        }

        //通过月份查询数据
        List<Integer> counts= null;
        try {
            counts = memberService.findCountByMonth(queryDataParm);
            Map<String, Object> map = new HashMap<>();
            map.put("memberCount", counts);
            map.put("months", queryDataParm);

            return Result.success(MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            log.error("Get memberCount error.",e);
            return Result.success(MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }
}